<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/public/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/public/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
                <table class="layui-hide" id="test-table-reload" lay-filter="test-table-reload"></table>

                <script type="text/html" id="test-table-reload-toolbar">
                    <button class="layui-btn layui-btn-sm #{if !toolsUtils.checkBtn('/role/add') } layui-hide #{/if}" lay-event="add"><i class="layui-icon layui-icon-add-circle-fine"></i>添加角色</button>
                </script>

                <script type="text/html" id="test-table-reload-barDemo">
                    <a class="layui-btn layui-btn-xs #{if !toolsUtils.checkBtn('/role/edit') } layui-hide #{/if}" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs #{if !toolsUtils.checkBtn('/role/deleteRole') } layui-hide #{/if}" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                </script>
            </div>
        </div>
    </div>
</div>

<script src="/public/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/public/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'utils', 'form'], function () {
        var $ = layui.$,
                table = layui.table,
                form = layui.form,
                admin = layui.admin;

        //监听搜索
        form.on("submit(LAY-search)", function (data) {
            var field = data.field;

            //执行重载
            table.reload('test-table-reload', {
                where: field
            });
        });

        //方法级渲染
        table.render({
            elem: '#test-table-reload'
            , url: "/role/getRoleList"
            , toolbar: '#test-table-reload-toolbar'
            , defaultToolbar: ['filter']
            , cellMinWidth: 80
            , cols: [[
                {field: 'id', title: 'ID', fixed: true}
                , {field: 'name', title: '角色名称'}
                , {field: 'userNumber', title: '角色账号数'}
                , {field: 'createName', title: '创建人'}
                , {field: 'createTime', title: '创建时间', sort: true, templet: function (d) {
                        return "<div>" + layui.toDateString(d.createTime) + "</div>";
                    }}
                , {field: 'modifyName', title: '修改人'}
                , {field: 'modifyTime', title: '修改时间', sort: true, templet: function (d) {
                        return "<div>" + layui.toDateString(d.modifyTime) + "</div>";
                    }}
                , {fixed: 'right', title: '操作', toolbar: '#test-table-reload-barDemo', width: 150}
            ]]
            , page: true
            , height: 520
        });

        //头工具栏事件
        table.on('toolbar(test-table-reload)', function (obj) {
            if (obj.event === 'add') {
                window.location.href = "/role/add";
            }
        });

        //监听行工具事件
        table.on('tool(test-table-reload)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                if(data.userNumber>0){
                    layer.msg("角色存在账号数,不能删除!", {icon: 2});
                    return false;
                }
                layer.confirm('真的删除这个角色么', {icon: 7, title: '提示', skin: 'layui-layer-molv', anim: 4}, function (index) {
                    obj.del();
                    layer.close(index);
                    $.get("/role/deleteRole?id=" + data.id, function (result) {
                        if (result.code > 0) {
                            layer.msg(result.msg, {icon: 2});
                            return false;
                        } else {
                            obj.del();
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                window.location.href = "/role/edit?id=" + data.id;
            }
        });

    });
</script>
</body>
</html>